package com.test.web.excel.listener;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.he.core.excle.listener.DefaultReadListener;
import com.test.web.excel.data.dl.ReadDlData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author heyanjing
 * date:2020-05-08 2020/5/8:9:55
 * 复写DefaultReadListener
 */
@Slf4j
@Data
public class DlReadListener extends DefaultReadListener<ReadDlData> {


    protected List<ReadDlData> data = new ArrayList<>();


    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(ReadDlData t, AnalysisContext analysisContext) {
        Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
        log.info("解析第{}行数据：{}", rowIndex, JSONUtil.toJsonStr(t));
        if (data.isEmpty()) {
            data.add(t);
        } else {
            Date dataDate = t.getDate();
            ReadDlData lastData = data.get(data.size() - 1);
            Date lastDataDate = lastData.getDate();
            for (int i = 10; i <= 60; i++) {
                DateTime needDate = DateUtil.offsetMinute(lastDataDate, i);
                if (DateUtil.compare(needDate, dataDate) <= 0) {
                    data.add(t);
                    break;
                }
            }
        }
    }

}
